package com.itheima.consumer.config;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 通过配置类来声明队列和交换机，队列和交换机的声明一般是在消费者中。
 */
@Configuration
public class FanoutConfiguration {

    // 1.定义一个Spring配置类中的Bean方法，用于创建并返回一个RabbitMQ的广播式交换器（Fanout Exchange）
    @Bean
    public FanoutExchange fanoutExchange() {
        // return ExchangeBuilder.fanoutExchange("hmall.fanout").build();
        return new FanoutExchange("hmall.fanout");
    }

    // 2.定义Queue的Bean方法，创建并返回一个RabbitMQ的队列
    @Bean
    public Queue fanoutQueue1() {
        // return QueueBuilder.durable("fanout.queue1").build();
        return new Queue("fanout.queue1");
    }

    @Bean
    public Queue fanoutQueue2() {
        return new Queue("fanout.queue2");
    }

    // 3.定义Binding的Bean方法，将RabbitMQ的交换机和队列进行绑定
    @Bean
    public Binding fanoutQueue1Binding(Queue fanoutQueue1, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);
    }

    @Bean
    public Binding fanoutQueue2Binding(Queue fanoutQueue2, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);
    }
}
